package cn.tedu.note.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.tedu.note.dao.UserDao;
import cn.tedu.note.entity.User;
import cn.tedu.note.service.PasswordException;
import cn.tedu.note.service.UserNameException;
import cn.tedu.note.service.UserNotFoundException;
import cn.tedu.note.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Resource(name="userDao")
	private UserDao userDao;
	
	@Value("#{jdbc.salt}")
	private String salt;
	
	
	public User login(String name, String password) 
			throws UserNotFoundException, PasswordException {
		
		if(password==null||password.trim().isEmpty()){
			throw new PasswordException("密码为空");
		}
		
		if(name==null||name.trim().isEmpty()){
			throw new UserNotFoundException("用户名为空");
		}
		
		User user = userDao.findUserByName(name.trim());
		
		if(user==null){
			throw new UserNotFoundException("用户名错误");
		}
		
		String pwd=DigestUtils.md5Hex(salt+password.trim());
		
		if(pwd.equals(user.getPassword())){
			return user;
		}
		
		throw new PasswordException("密码错误");
	
	}

	public User regist(
			String name, String nick, 
			String password, String confirm)
			throws UserNameException, PasswordException {
		String id=UUID.randomUUID().toString();
		//检查name,不能重复
		if(name==null||name.trim().isEmpty()){
			throw new UserNameException("用户名为空");
		}
		User one = userDao.findUserByName(name);
		if(one!=null){
			throw new UserNameException("用户名已存在");
		}
		//检查密码	
		if(password==null||password.trim().isEmpty()){
			throw new PasswordException("密码不能为空");
		}
		
		if(!password.equals(confirm)){
			throw new PasswordException("确认密码错误");
		}
		//检查nick(昵称不存在时,将name作为昵称)
		if(nick==null||nick.trim().isEmpty()){
			nick=name;
		}
		String token="";
		String pwd=DigestUtils.md5Hex(salt+password.trim());
		User user = new User(id,name,pwd,token,nick);
		int n = userDao.addUser(user);
		if(n!=1){
			throw new RuntimeException("添加失败");
		}
		
		
		return user;
	}

}









